<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>guestbook - register</title>
<?php
session_cache_limiter('private, must-revalidate');
// 防止验证码出错后清空数据。
session_start();
define('IN_TG', true);
define('SCRIPT', 'register');
require_once dirname(__FILE__) . '/includes/common.inc.php';
require_once ROOT_PATH . 'includes/title.inc.php';
_login_state();
  global $_link;//仅用来消除本页面，变量没定义的警告
// mysqli_query("INSERT INTO tg_user(tg_username) VALUES ('test')") or die('SQL');
// echo $_clean['active'] = _sha1_uniqid();
if ($_GET['action'] == 'register') {
    _check_code($_POST['code'], $_SESSION['code']);
    include_once ROOT_PATH . 'includes/register.func.php';
    $_clean = array();
    // 唯一标识符,cookie登陆验证
    $_clean['uniqid'] = _check_uniqid($_POST['uniqid'], $_SESSION['uniqid']);
    // 激活用户
    $_clean['active'] = _sha1_uniqid();
    $_clean['name'] = _check_name($_POST['name']);
    $_clean['password'] = _check_password($_POST['password'], $_POST['passworded']);
    $_clean['question'] = _check_question($_POST['question']);
    $_clean['answer'] = _check_answer($_POST['answer'], $_POST['question']);
    $_clean['sex'] = $_POST['sex'];
    $_clean['face'] = $_POST['facesrc'];
    $_clean['email'] = _check_email($_POST['email']);
    $_clean['qq'] = _check_qq($_POST['qq']);
    $_clean['url'] = _check_url($_POST['url']);
    
    $_sql = "SELECT `tg_username` FROM `tg_user` WHERE `tg_user`.`tg_username` ='{$_clean['name']}' LIMIT 1";
    _is_repeat($_link, $_sql, '用户名相同');
    $_sql = "INSERT INTO `tg_user` (
    `tg_id`,
    `tg_uniqid`,
    `tg_active`,
    `tg_username`,
    `tg_password`,
    `tg_question`,
    `tg_answer`,
    `tg_email`,
    `tg_qq`,
    `tg_url`,
    `tg_sex`,
    `tg_face`,
    `tg_reg_time`,
    `tg_last_time`,
    `tg_last_ip`) VALUES (
    '',
    '{$_clean['uniqid']}',
    '{$_clean['active']}',
    '{$_clean['name']}',
    '{$_clean['password']}',
    '{$_clean['question']}',
    '{$_clean['answer']}',
    '{$_clean['email']}',
    '{$_clean['qq']}',
    '{$_clean['url']}',
    '{$_clean['sex']}',
    '{$_clean['face']}',
    NOW(),
    NOW(),
    '{$_SERVER["REMOTE_ADDR"]}')";
    _query($_link, $_sql);
    if (mysqli_affected_rows($_link) == 1) {
        mysqli_close($_link);
        //销毁session
        session_destroy();
        _location('active.php?active='.$_clean['active'], '注册成功！');
    } else {
        mysqli_close($_link);
        //销毁session
        session_destroy();
        _location('register.php', '注册失败，请重新注册！');
    }
} else {
    $_SESSION['uniqid'] = $_uniqid = _sha1_uniqid();
}
?>
<script type="text/javascript" src="js/register.js"></script>
</head>
<body>

<?php
require_once ROOT_PATH . 'includes/header.inc.php';
?>

<div id="register">
		<h2>会员注册</h2>
		<form method="post" name="register"
			action="register.php?action=register">
			<input type="hidden" name="uniqid" value="<?php echo $_uniqid;?>" />
			<ul>
				<li id="title">请填写以下内容</li>
				<li><label>用户名称：</label><input type="text" name="name" class="text" />(*必填，至少两位)</li>
				<li><label>输入密码：</label><input type="password" name="password"
					class="text" />(*必填，至少六位)</li>
				<li><label>确认密码：</label><input type="password" name="passworded"
					class="text" />(*必填，至少六位)</li>
				<li><label>密码提示：</label><input type="text" name="question"
					class="text" />(*必填，至少两位)</li>
				<li><label>密码回答：</label><input type="text" name="answer"
					class="text" />(*必填，至少两位)</li>
				<li><label>性别：</label><input type="radio" name="sex" value="1"
					checked />男 <input type="radio" name="sex" value="0" />女</li>
				<li><label>头像选择：</label><input type="hidden" name="facesrc"
					class="face" value="face/m01.jpg" /><img class="face"
					src="face/m01.jpg" alt="头像选择" id="face" /></li>
				<li><label>电子邮件：</label><input type="text" name="email" class="text" />(*必填)</li>
				<li><label>QQ：</label><input type="text" name="qq" class="text" /></li>
				<li><label>主页地址：</label><input type="text" name="url" class="text"
					value="http://" /></li>
				<li><label>验证码：</label><input type="text" name="code"
					class="text yzm" /><img id="code" src="code.php" /></li>
				<li><input type="submit" class="submit" value="注册" /></li>
			</ul>
		</form>
	</div>
<?php
require_once ROOT_PATH . 'includes/footer.inc.php';
?>
</body>
</html>